package com.zlict.admin.service.impl;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zlict.common.utils.DateUtils;
import com.zlict.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zlict.admin.mapper.UserSalaryMapper;
import com.zlict.admin.domain.UserSalary;
import com.zlict.admin.service.IUserSalaryService;

/**
 * 研发人员工资Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-09
 */
@Service
public class UserSalaryServiceImpl implements IUserSalaryService 
{
    @Autowired
    private UserSalaryMapper userSalaryMapper;

    /**
     * 查询研发人员工资
     * 
     * @param id 研发人员工资主键
     * @return 研发人员工资
     */
    @Override
    public UserSalary selectUserSalaryById(Long id)
    {
        return userSalaryMapper.selectUserSalaryById(id);
    }

    /**
     * 查询研发人员工资列表
     * 
     * @param userSalary 研发人员工资
     * @return 研发人员工资
     */
    @Override
    public List<UserSalary> selectUserSalaryList(UserSalary userSalary)
    {
        return userSalaryMapper.selectUserSalaryList(userSalary);
    }

    /**
     * 新增研发人员工资
     * 
     * @param userSalary 研发人员工资
     * @return 结果
     */
    @Override
    public int insertUserSalary(UserSalary userSalary)
    {
        userSalary.setCreateTime(DateUtils.getNowDate());
        return userSalaryMapper.insertUserSalary(userSalary);
    }

    /**
     * 修改研发人员工资
     * 
     * @param userSalary 研发人员工资
     * @return 结果
     */
    @Override
    public int updateUserSalary(UserSalary userSalary)
    {
        userSalary.setUpdateTime(DateUtils.getNowDate());
        return userSalaryMapper.updateUserSalary(userSalary);
    }

    /**
     * 批量删除研发人员工资
     * 
     * @param ids 需要删除的研发人员工资主键
     * @return 结果
     */
    @Override
    public int deleteUserSalaryByIds(Long[] ids)
    {
        return userSalaryMapper.deleteUserSalaryByIds(ids);
    }

    /**
     * 删除研发人员工资信息
     * 
     * @param id 研发人员工资主键
     * @return 结果
     */
    @Override
    public int deleteUserSalaryById(Long id)
    {
        return userSalaryMapper.deleteUserSalaryById(id);
    }

    /**
     * 导入工资数据
     *
     * @param salaryList 工资数据列表
     * @param updateSupport 是否支持更新，如果已存在，则进行更新数据
     * @return 结果
     */
    @Override
    public Map<String,Object> importSalary(List<UserSalary> salaryList, boolean updateSupport)
    {
        if (StringUtils.isNull(salaryList) || salaryList.size() == 0)
        {
            throw new RuntimeException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        for (UserSalary salary : salaryList)
        {
            try
            {
                // 验证是否存在该用户
                List<UserSalary> userList = userSalaryMapper.yzry(salary);
                if (userList.size() == 0)
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、工号 " + salary.getUserName() + " 不存在");
                    continue;
                }

                // 验证该月份是否已存在数据
                List<UserSalary> existList = userSalaryMapper.yzyf(salary);
                if (existList.size() > 0)
                {
                    if (updateSupport)
                    {
                        salary.setUpdateTime(DateUtils.getNowDate());
                        updateUserSalary(salary);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、工号 " + salary.getUserName() + " 更新成功");
                    }
                    else
                    {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、工号 " + salary.getUserName() + "在"+sdf.format(salary.getSalaryDate()) +"月份已导入过工资,无法重复导入");
                    }
                    continue;
                }

                salary.setCreateTime(DateUtils.getNowDate());
                insertUserSalary(salary);
                successNum++;
                successMsg.append("<br/>" + successNum + "、工号 " + salary.getUserName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、工号 " + salary.getUserName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }

        Map<String,Object> map= new HashMap<String,Object>();
        if (failureNum > 0)
        {
            failureMsg.insert(0, "导入成功"+successNum+"条,导入失败" + failureNum + "条,数据格式不正确,错误如下：");
           // throw new RuntimeException(failureMsg.toString());
            map.put("successNum",successNum);
            map.put("failureNum",failureNum);
            map.put("failureMsg",failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
            map.put("successNum",successNum);
            map.put("failureNum",failureNum);
            map.put("failureMsg",failureMsg.toString());
        }

//        map.put("successNum",successNum);
//        map.put("failureNum",failureNum);
//        map.put("failureMsg",failureMsg.toString());
       // return successMsg.toString();
        return map;
    }
}
